package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.RedisUtil;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.ReSeAddress;
import com.chinairi.mall.dao.ReSeAddressMapper;
import com.chinairi.mall.service.IReSeAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinairi.mall.service.modelobject.PurchaseToProjectRedisMO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 收/发货地址表 服务实现类
 * </p>
 *
 * @author gsl
 * @since 2020-09-16
 */
@Service
public class ReSeAddressServiceImpl extends ServiceImpl<ReSeAddressMapper, ReSeAddress> implements IReSeAddressService {

    @Autowired
    private ReSeAddressMapper reSeAddressMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 新增收货地址
     * @param reSeAddress
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ReSeAddress reSeAddress, HttpServletRequest request) {
        reSeAddress.setRsaId(UUID.randomUUID().toString().replace("-", ""));
        String userId = UserSession.getUserId();
        if(StringUtils.isEmpty(userId)){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"用户token无效");
        }
        reSeAddress.setRsaUserCode(userId);
        reSeAddress.setRsaIsDefault("0");
        reSeAddressMapper.insert(reSeAddress);
    }

    /**
     * 获取用户的收发货地址
     *
     * @param request
     * @return
     */
    @Override
    @DoPage
    public List<ReSeAddress> get(HttpServletRequest request) {
        String userId = UserSession.getUserId();
        if(StringUtils.isEmpty(userId)){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"用户token无效");
        }
        QueryWrapper<ReSeAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("RSA_USER_CODE", userId);
        return reSeAddressMapper.selectList(queryWrapper);
    }
}
